package com.cchr.acms.util;

import java.util.List;

import com.alibaba.druid.util.StringUtils;

public class HtmlComponentUtil {

	public static String getSelect(String id, String name, List<SelectOption> data, String selected, String placeholder) 
	{
		StringBuilder sb = new StringBuilder();
		sb.append("<select id=\"").append(id).append("\" name=\"").append(name).append("\" ");
		if(!StringUtils.isEmpty(placeholder))
		{
			sb.append(" data-placeholder=\"").append(placeholder).append("\" ");
		}
		sb.append("\" >");
		if(!StringUtils.isEmpty(placeholder))
		{
			SelectOption op = new SelectOption();
			op.setLabel("");
			op.setValue("");
			generateOption(sb, op, selected);
		}
		if(data != null)
		{
			for(SelectOption op : data)
			{
				generateOption(sb, op, selected);
			}
		}
		sb.append("</select>");
		return sb.toString();
	}
	
	private static void generateOption(StringBuilder sb, SelectOption op, String selected)
	{
		sb.append("<option");
		if(op.getValue() != null)
		{
			sb.append(" value=\"").append(op.getValue()).append("\"");
			if(op.getValue().equals(selected))
			{
				sb.append(" selected=\"selected\"");
			}
		}
		if(op.disabled)
		{
			sb.append(" disabled ");
		}
		sb.append(">");
		if(op.getLabel() != null)
		{
			sb.append(op.getLabel());
		}
		sb.append("</option>");
	}
	
	public static class SelectOption
	{
		String value;
		String label;
		String group;
		boolean disabled = false;
		
		public boolean isDisabled() {
			return disabled;
		}
		public void setDisabled(boolean disabled) {
			this.disabled = disabled;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
		public String getLabel() {
			return label;
		}
		public void setLabel(String label) {
			this.label = label;
		}
		public String getGroup() {
			return group;
		}
		public void setGroup(String group) {
			this.group = group;
		}
	}

}
